package cn.itcast.mq.amqp;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

/**
 * @Author: ahcfl
 * @Date: 2021年07月18日 17:00
 * @Description:
 */
@Component
public class SpringRabbitListener {

    // basicQueue
    /*@RabbitListener(queues = "simple.queue")
    public void listenerSimpleQueueMessage(String msg){
        System.out.println("spring消费者接收到msg = " + msg);
    }*/

    // 接受workQueue消息
    // 平均分配给每个消费者，[并没有考虑到消费者的处理能力]
    // 能者多劳 多个消费者共同处理消息处理 消除消息堆积
    //    prefetch: 1 # 每次只能获取一条消息，处理完成才能获取下一个消息
    @RabbitListener(queues = "simple.queue")
    public void listenerQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息msg = " + msg + "：" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenerQueue2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到消息msg = " + msg + "：" + LocalTime.now());
        Thread.sleep(200);
    }


    // 广播模式
    /*@RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg){
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg){
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }*/

    // 定向模式  不同的消息被不同的队列消费
    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","green"}
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者接收到direct.queue1的消息 = " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("消费者接收到direct.queue2的消息 = " + msg);
    }*/

    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者接收到topic.queue1的消息 = " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息 = " + msg);
    }

    @RabbitListener(queuesToDeclare = @Queue(name = "object.queue"))
    public void listenObjectQueue(Message message){
        System.out.println("message = " + message);
        System.out.printf(message.getBody().toString());
    }*/

}
